﻿// ==================================================
// Copyright 2014 (C) , dotLogix
// Author: Alexander Schill <alexander@schillnet.de>.
// ==================================================

namespace DotLogix {
    #region Using Directives
    using System;
    using System.Windows;
    using System.Windows.Media;

    #endregion

    public class AutoSizePathButton : AutoSizePath {
        private Brush _background = Brushes.Transparent;
        private TranslateTransform _translate;
        public Brush Background {
            get { return _background; }
            set {
                _background = value;
                InvalidateVisual();
            }
        }

        protected override void OnRender(DrawingContext drawingContext) {
            drawingContext.DrawRectangle(_background, null, new Rect(DesiredSize));
            drawingContext.PushTransform(_translate);
            base.OnRender(drawingContext);
            drawingContext.Pop();
        }

        protected override Size MeasureOverride(Size availableSize) {
            var neededSize = base.MeasureOverride(availableSize);
            double translateX, translateY;
            switch(HorizontalAlignment) {
                case HorizontalAlignment.Left:
                    translateX = 0;
                    break;
                case HorizontalAlignment.Stretch:
                case HorizontalAlignment.Center:
                    translateX = (availableSize.Width - neededSize.Width) / 2;
                    break;
                case HorizontalAlignment.Right:
                    translateX = availableSize.Width - neededSize.Width;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            switch(VerticalAlignment) {
                case VerticalAlignment.Top:
                    translateY = 0;
                    break;
                case VerticalAlignment.Stretch:
                case VerticalAlignment.Center:
                    translateY = (availableSize.Height - neededSize.Height) / 2;
                    break;
                case VerticalAlignment.Bottom:
                    translateY = availableSize.Height - neededSize.Height;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            _translate=new TranslateTransform(translateX,translateY);
            return availableSize;
        }
    }
}